package com.nisco.ampf;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.TextView;

public class MenuActivity extends Activity {

	private TextView resultView;
	private MenuItem helpItem;
	private MenuItem aboutItem;
	private MenuItem exitItem;
	private Button stackManageBtn;
	private Button slabMangeBtn;
	//示例
	private Button menuImgBtn3;
    private String loginName;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_menu);
		resultView = (TextView) findViewById(R.id.resultView);
		stackManageBtn = (Button) findViewById(R.id.stackManageBtn);
		slabMangeBtn = (Button) findViewById(R.id.slabMangeBtn);
		
		//找到布局文件相对应的button
		menuImgBtn3=(Button) findViewById(R.id.menuImgBtn3);
		
		Intent intent = getIntent();
		loginName = intent.getStringExtra("username");
		resultView.setText(loginName
				+ ",欢迎使用板卷厂辅助管理系统！");
		helpItem = (MenuItem) findViewById(R.id.help);
		aboutItem = (MenuItem) findViewById(R.id.about);
		exitItem = (MenuItem) findViewById(R.id.exit);
		stackManageBtn.setOnTouchListener(new menuBtnTouchListener());
		stackManageBtn.setOnClickListener(new menuBtnClickListener());
		slabMangeBtn.setOnTouchListener(new menuBtnTouchListener());
		slabMangeBtn.setOnClickListener(new menuBtnClickListener());
		//给menuImgBtn3添加click事件
		menuImgBtn3.setOnClickListener(new menuBtnClickListener());
	}

	/**
	 * 在menu页面增加菜单按钮
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu, menu);
		return true;
	}

	/**
	 * 定义菜单按钮的触发事件
	 */
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.help:
			return true;
		case R.id.exit:
			this.finish();
			return true;
		case R.id.about:
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setTitle("关于");
			builder.setMessage("该系统由财务协同组成员开发，欢迎提出宝贵意见。\n谢谢！")
					.setCancelable(false)
					.setPositiveButton("确定",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int id) {
									dialog.cancel();
								}
							});
			AlertDialog alert = builder.create();
			alert.show();
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	class menuBtnClickListener implements OnClickListener {

		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.stackManageBtn:
				
				Intent intent = new Intent(MenuActivity.this,
						StackManagerActivity.class);
				intent.putExtra("loginName", loginName);
				startActivity(intent);
				v.setBackgroundColor(Color.TRANSPARENT);
				break;
			case R.id.slabMangeBtn:
				Intent intent2 = new Intent(MenuActivity.this,
						SlabManagerActivity.class);
				intent2.putExtra("loginName", loginName);
				startActivity(intent2);
				v.setBackgroundColor(Color.TRANSPARENT);
				break;
			case R.id.menuImgBtn3:
				Intent intent3 = new Intent(MenuActivity.this,
						Search1ManagerActivity.class);
				intent3.putExtra("loginName", loginName);
				startActivity(intent3);
				v.setBackgroundColor(Color.TRANSPARENT);
				break;
			default:
				break;
			}

		}

	}
	class menuBtnTouchListener implements OnTouchListener{


		public boolean onTouch(View v, MotionEvent event) {
			v.setBackgroundColor(Color.GREEN);
			return false;
		}
		
	}
 
	/**
	 * 触发返回键的相关事件
	 */
	@Override
	public boolean onKeyDown(int arg0, KeyEvent arg1) {
		if (arg0 == KeyEvent.KEYCODE_BACK && arg1.getRepeatCount() == 0) {
			//点击返回键调用的方法
			backDialog();
			return false;
		}
		return false;
	}

	/**
	 * 返回键对话框
	 */
	public void backDialog() {
		AlertDialog.Builder builder = new Builder(MenuActivity.this);
		builder.setMessage("确定要退出吗？");
		builder.setTitle("提示");
		builder.setPositiveButton("确认",
				new android.content.DialogInterface.OnClickListener() {


					public void onClick(DialogInterface arg0, int arg1) {
						arg0.dismiss();
						MenuActivity.this.finish();

					}
				});
		builder.setNegativeButton("取消",
				new android.content.DialogInterface.OnClickListener() {


					public void onClick(DialogInterface arg0, int arg1) {
						arg0.dismiss();
					}
				});
		builder.create().show();
	}
}
